from django.db import models

# Create your models here.
'''
用户表（UserInfo）、角色表（RoleInfo）、菜单表（MenuInfo）、用户角色表（UserRole）、角色菜单表（RoleMenu）
'''


class Menu(models.Model):
    name = models.CharField(verbose_name='菜单名称', max_length=50)

    # icon = models.CharField(verbose_name='菜单图标')

    class Meta:
        db_table = "rbac_menu"
        verbose_name = "菜单"


class PermissionGroup(models.Model):
    caption = models.CharField(verbose_name='部门', max_length=50)
    menu = models.ForeignKey(Menu, verbose_name='所属菜单', on_delete=models.PROTECT)

    class Meta:
        db_table = "rbac_permission_group"
        verbose_name = "权限组"


class Permission(models.Model):
    METHOD_POST = 'POST'
    METHOD_GET = 'GET'
    METHOD_DELETE = 'DELETE'
    METHOD_PATCH = 'PATCH'
    METHOD_CHOICES = (
        (METHOD_POST, '添加'),
        (METHOD_GET, '查看'),
        (METHOD_DELETE, '删除'),
        (METHOD_PATCH, '修改'),
    )

    METHOD_CHOICE_DICT = [{'code': item[0], 'codeName': item[1]} for item in METHOD_CHOICES]

    title = models.CharField(verbose_name='权限名称', max_length=20)
    url = models.CharField(verbose_name='包含正则的url', max_length=200)
    method = models.CharField(choices=METHOD_CHOICES, verbose_name='请求方式', max_length=20, default=METHOD_GET)
    group = models.ForeignKey(PermissionGroup, verbose_name='所属群组', on_delete=models.PROTECT)

    # group_menu = models.ForeignKey('Permission', verbose_name='组内菜单', null=True, blank=True,
    #                                on_delete=models.PROTECT)

    class Meta:
        db_table = "rbac_permission"
        verbose_name = "权限"


class Role(models.Model):
    title = models.CharField(verbose_name='角色名称', max_length=20)
    permission = models.ManyToManyField(Permission, verbose_name='拥有的权限')

    class Meta:
        db_table = "rbac_role"
        verbose_name = "角色"
